package com.hs.chd.helper;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class TextHelper {
	/**
	 * 导出通用接口
	 * @param title 表格标题名
	 * @param headers 表格属性列名数组
	 * @param dataset 需要显示的数据集合
	 * @param out 与输出设备关联的流对象
	 * @throws IOException
	 */
	public static void exportTxt(String title, String[] headers,
			Collection<String[]> dataset, OutputStream out) throws IOException {
		StringBuffer expBuffer = new StringBuffer();
        // 遍历集合数据，产生数据行
        Iterator<String[]> itor = dataset.iterator();
        String textValue = "";
        while(itor.hasNext()){
        	String[] dataArray = itor.next();
        	for(int i=0,c=dataArray.length;i<c;i++){
        		textValue = dataArray[i];
        		if(textValue==null)
        			textValue = "无";
        		expBuffer.append(headers[i]).append(":").append(replaceBlank(textValue)).append("\r\n");
        	}
        	expBuffer.append("\n");
        }
        out.write(expBuffer.toString().getBytes());
	}
	
	
	/**
	 * 去除字符串中的空格、回车、换行符、制表符
	 * \\s*|\t|\r|\n
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {  
		String dest = "";  
		if (str!=null) {  
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
			Matcher m = p.matcher(str);  
			dest = m.replaceAll("");  
		}  
		return dest;  
	}  


}
